package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import com.limao.DBDataClass.UserRedBagDataClass;
import com.limao.DBDataClass.VrGoodsDataClass;
import com.limao.DBDataClass.VrGoodsNumClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;


public class VrGoodsUserNumProvider {

	private static class DB_Adv_ProviderContainer {
		private static VrGoodsUserNumProvider instance = new VrGoodsUserNumProvider();
	}
	
	public static VrGoodsUserNumProvider getInstance() {
		return DB_Adv_ProviderContainer.instance;
	}
	
	// 初始化
	private VrGoodsUserNumProvider() {
	
	}
	
	private VrGoodsNumClass encapOrderData(ResultSet rs) throws Exception{
		
		VrGoodsNumClass orderData = new VrGoodsNumClass();
		orderData.userid= rs.getString("userid");
		orderData.vrgoodsid= rs.getString("vrgoodsid");
		orderData.num= rs.getString("num");
		orderData.num15days= rs.getString("num15days");
		return orderData;
		
	}
	
	public List<VrGoodsDataClass> getUserVrGoodsNum(List<VrGoodsDataClass> list,int userid)
	{
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_user_num where userid=?");
			pstmt.setInt(1, userid);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				int vrgoodsid=rs.getInt("vrgoodsid");
				for(VrGoodsDataClass obj:list)
				{
					if(vrgoodsid==CacheClass.getIntValue(obj.vrgoodsid))
					{
						obj.usergoodsnum=rs.getString("num");
						obj.num15days=rs.getString("num15days");
					}
				}
			}
		} catch (Exception e) {
			list.clear();
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return list;
	}
	
	//取15天前的某现货瓶数
	public VrGoodsNumClass getUserVrGoodsNum(int vrgoodsid,int userid)
	{
		VrGoodsNumClass tmp=null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_user_num where userid=? and vrgoodsid=?");
			pstmt.setInt(1, userid);
			pstmt.setInt(2, vrgoodsid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp=encapOrderData(rs);
			}
		} catch (Exception e) {
			tmp=null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	//当前现货总瓶数
	public float getUserVrGoodsNum(String userid)
	{
		float num=0;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select sum(num) as vrgoodsnum from cd_user_num where userid=? group by userid");
			pstmt.setInt(1, CacheClass.getIntValue(userid));
			rs = pstmt.executeQuery();
			if (rs.next()) {
				num=rs.getFloat("vrgoodsnum");
			}
		} catch (Exception e) {
			num=0;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return num;
	}
	

}
